<?php
// +----------------------------------------------------------------------
// | WoShiMaiJia Projcet
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <517577550@qq.com>
// +----------------------------------------------------------------------
/**
 * 元数据基类
 * 提供了缓存操作功能及基本的CRUD操作功能
 * @author xinqiyang
 *
 */
class MetaModel extends Model
{
	//缓存实例
	protected $cache;
	/**
	 * 返回一个缓存操作实例
	 */
	function _initialize()
	{
		//如果未设置则设置缓存实例
		if(!isset($this->cache))
		{
			$this->cache = Cache::getInstance('Memcache');
		}
	}

	/**
	 * 删除缓存
	 * @param $cachename 缓存名称
	 */
	protected function rm($cachename)
	{
		$this->cache->rm($cachename);
	}



	/**
	 * 获取单条数据
	 * @param unknown_type $map 查询条件
	 * @param unknown_type $field 字段
	 */
	public function get($map,$field="*")
	{
		//返回搜索结果
		return $this->where($map)->field($field)->find();
	}
	/**
	 * 获取数据集
	 * @param unknown_type $map 查询条件
	 * @param unknown_type $order 排序字段
	 * @param unknown_type $field 字段
	 * @param unknown_type $group 分组
	 */
	public function result($map,$order,$field="*",$group='')
	{
		return $this->where($map)->field($field)->order($order)->group($group)->select();
	}

	/**
	 * 公共插入类
	 *
	 */
	public function ins()
	{
		if($this->create())
		{
			//添加数据
			$id = $this->add();
			if($id)
			{
				//TODO:添加成功,更新缓存
				return $id;
			}
		}else{
			return $this->getError();
		}
	}
	/**
	 * 公共修改类
	 */
	public function upd()
	{
		if($this->create())
		{
			//保存数据
			$id=$this->save();
			if($id)
			{
				//TODO:保存成功,更新缓存
				return $id;
			}
		}else{
			return $this->getError();
		}
	}

	/**
	 * 公共的删除方法
	 * 只修改标志位信息,不删除数据库
	 * TODO:这里进行修改
	 */
	public function del() {
		$data['id'] = $_POST['id'];
		$data['status'] = 2;
		//保存数据
		$id=$this->save($data);
		if($id)
		{
			//TODO:保存成功,更新缓存
			return $id;
		}
	}

	/**
	 * 获取单个实例,用于填出新建字段
	 * by xinqiyang
	 */
	public function getOne()
	{
		$arr = $this->getDbFields();
		$rarr = array();
		foreach($arr as $key=>$value)
		{
			if(is_int($key))
			{
				$arrs = array($value=>'');
				$rarr += $arrs;
			}
		}
		return $rarr;
			
	}

	/**
	 * 判断是否存在
	 * @param unknown_type $model 模型
	 * @param unknown_type $id ID
	 */
	public function exist($model,$id)
	{
		$id = M($model)->field('id')->find($id);
		if($id['id'])
		{
			return $id['id'];
		}
		return false;
	}

	/**
	 * 获取模型字段的内容
	 * @param unknown_type $model 模型名称
	 * @param unknown_type $map   条件集
	 * @param unknown_type $field 字段名
	 */
	public function getFieldValue($model,$map,$field='id')
	{
		return M($model)->where($map)->field($field)->find();
	}

	/**
	 * 获取数据库表
	 */
	public function getModels()
	{
		$table_arr = array();
		$dbname = 'Tables_in_'.C('DB_NAME');
		$dbpre = C('DB_PREFIX');
		$tables = $this->query('SHOW TABLES');
		foreach($tables as $value)
		{
			$tbname = str_replace($dbpre,'',$value[$dbname]);
			array_push($table_arr,$tbname);
		}
		return $table_arr;
	}
	
	
	public function getUserAndModel($model,$model_id,$type)
	{
		return $this->query("
		SELECT sz_user.id, sz_user.cnname, sz_image.url, sz_image.filename, sz_image.model
		FROM sz_user, sz_userandmodel, sz_image
		WHERE sz_user.image_id = sz_image.id
		AND sz_userandmodel.user_id = sz_user.id
		AND sz_userandmodel.model =  '$model'
		AND sz_userandmodel.model_id =  '$model_id'
		AND sz_userandmodel.linktype =  '$type'
		order by rand()
		LIMIT 0 , 10");
	}


}
?>